package com.swan.core.utils.file;

import com.swan.core.enums.SwanExCodeEnum;
import com.swan.core.exception.SwanBaseException;
import com.swan.core.utils.io.IOUtils;
import com.swan.core.utils.collection.ListUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**文本文件读写, 当文件内容过大时, 需要考虑内存
 * @author zongf
 * @since 2021-01-11
 */
@Slf4j
public class TxtFileUtil {

    /** 写入文件
     * @param filePath 文件路径
     * @param contents 文件内容
     * @param autoCreateDir 是否自动创建文件夹
     */
    public static void writeFile(String filePath, boolean autoCreateDir,  String... contents) {

        // 监测目录是否存在
        File targetFile = new File(filePath);
        if (!targetFile.getParentFile().exists()) {
            if (autoCreateDir) {
                targetFile.getParentFile().mkdirs();
                log.info("目录不存在, 准备创建目录:{}", targetFile.getAbsolutePath());
            }
        }

        // 写入文件
        writeFile(filePath, contents);
    }

    /** 写入文件
     * @param filePath 文件路径
     * @param contents 文件内容
     * @author zongf
     * @since 2021-01-11
     */
    public static void writeFile(String filePath, String... contents ) {

        File file = new File(filePath);

        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8"));
            for (int i = 0; i < contents.length; i++) {
                String content = contents[i];
                bw.write(content);
                if (i < contents.length-1) {
                    bw.newLine();
                }
            }
        } catch (IOException e) {
            throw new SwanBaseException(SwanExCodeEnum.UNKNOWN.code(), "文件写入失败, 文件名:" + filePath, e);
        }finally {
            IOUtils.close(bw);
        }
    }

    /** 写入文件
     * @param filePath 文件路径
     * @param lines 文件内容
     * @author zongf
     * @since 2021-01-11
     */
    public static void writeFile(String filePath, List<String> lines) {
        String[] strings = ListUtil.toArray(lines, String.class);
        writeFile(filePath, strings);
    }

    /** 读取文件内容
     * @param filePath 文件路径
     * @return 文件内容
     * @author zongf
     * @since 2022-11-26
     */
    public static List<String> readFile(String filePath) {
        BufferedReader br = null;

        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath)));

            List<String> lines = new ArrayList<>();

            String line = null;
            while ((line = br.readLine()) != null) {
                lines.add(line);
            }
            return lines;
        } catch (IOException e) {
            throw new SwanBaseException(SwanExCodeEnum.UNKNOWN.code(), "读取文件失败, 文件名:" + filePath, e);
        }finally {
            IOUtils.close(br);
        }
    }

    /** 读取文件内容
     * @param filePath 文件路径
     * @return 文件内容
     * @author zongf
     * @since 2022-11-26
     */
    public static String readFileContent(String filePath) {
        BufferedReader br = null;

        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath)));

            StringBuilder sb = new StringBuilder();

            String line = null;
            while ((line = br.readLine()) != null) {
                sb.append(line).append("\n");
            }
            return sb.toString();
        } catch (IOException e) {
            throw new SwanBaseException(SwanExCodeEnum.UNKNOWN.code(), "读取文件失败, 文件名:" + filePath, e);
        }finally {
            IOUtils.close(br);
        }
    }


}
